package com.anjiebang.mvc.base;

import com.anjiebang.conf.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统controller
 * Created by laiyanlin on 2018/4/8.
 */
public class BaseController {

    @Autowired
    protected SysConfig sysConfig;

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpSession session;

    /**
     * 成功
     */
    protected final static String SUCCESS = "success";

    /**
     * 失败
     */
    protected final static String ERROR = "error";

    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 创建返回APP对象
     * @return
     */
    protected Map<String, Object> getMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        return map;
    }

    /**
     * 前台返回json对象
     * @param code
     * @param msg
     * @param data
     * @return
     */
    protected RequestVo returnData(int code,String msg,Object data){
        return new RequestVo(code,msg,data);
    }

    /**
     * 前台返回json对象(成功)
     * @param data
     * @return
     */
    protected RequestVo returnData(Object data){
        return new RequestVo(0,"",data);
    }
    /**
     * 前台返回json对象(成功)
     * @param data
     * @return
     */
    protected RequestVo returnDataList(Object data){
        Map<String,Object> map = new HashMap<>();
        map.put("list",data);
        return new RequestVo(0,"",map);
    }

    /**
     * 前台返回json对象(成功)
     * @param data
     * @return
     */
    protected RequestVo returnDataObj(Object data){
        Map<String,Object> map = new HashMap<>();
        map.put("data",data);
        return new RequestVo(0,"",map);
    }



    /**
     * 获取header中userId参数
     * @return
     */
    protected String getToken() {
        return request.getHeader("token");
    }

    protected String getCityId() {
        return request.getHeader("cityId");
    }

    protected String getOs() {
        return request.getHeader("os");
    }

    protected String getOsVersion() {
        return request.getHeader("osVersion");
    }

    protected String getAppVersion() {
        return request.getHeader("appVersion");
    }

    protected String getServiceVersion() {
        return request.getHeader("serviceVersion");
    }

    protected Map<String,Object> getHeader() {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("token", getToken());
        map.put("cityId", getCityId());
        map.put("os", getOs());
        map.put("osVersion", getOsVersion());
        map.put("appVersion", getAppVersion());
        map.put("serviceVersion", getServiceVersion());
        return map;
    }

}
